﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace CommunitySystem.PageView.WordAction
{
    public partial class PositionChangeWindow : ChildWindow
    {
        private readonly WordActionService.WordActionClient _client = null;
        private CommunitySystemModel.WordMode.Silverlight.Word_XingZhengJieShaoLetter _clientEntity = null;
        private int oldYear = new int();
        private int oldNO = new int();
        private int errorCount = 0;
        public PositionChangeWindow(int id)
        {
            InitializeComponent();
            this.BindingValidationError += (sender, e) =>
            {
                if (e.Action == ValidationErrorEventAction.Added)
                {
                    errorCount++;
                }
                else if (e.Action == ValidationErrorEventAction.Removed)
                {
                    errorCount--;
                }
            };
            _client = new WordActionService.WordActionClient();
            _client.SaveWord_PostionChangeCompleted += (sender1, e1) =>
            {
                if (e1.Error == null)
                {
                    if (!string.IsNullOrEmpty(e1.Result))
                    {
                        string serverpath = _client.InnerChannel.Via.AbsoluteUri.Replace(_client.InnerChannel.Via.AbsolutePath, "");
                        System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(serverpath + e1.Result, UriKind.RelativeOrAbsolute));
                    }
                    else
                    {
                        SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功!");
                    }
                    this.DialogResult = true;
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage(e1.Error.Message);
                }
            };
            _client.GetWord_PostionChangeCompleted += (sender, e) =>
            {
                var entity = e.Result.FirstOrDefault();
                _clientEntity = CommunitySystem.Model.Silverlight.EntityConvertor.Convert<CommunitySystemModel.WordMode.Silverlight.Word_XingZhengJieShaoLetter>(entity);
                oldYear = (int)_clientEntity.Year;
                oldNO = (int)_clientEntity.NO;
                DataBinding();
            };
            if (id > 0)
            {
                _client.GetWord_PostionChangeAsync("ID=" + id);
            }
            else
            {
                _clientEntity = new CommunitySystemModel.WordMode.Silverlight.Word_XingZhengJieShaoLetter() {CreateDate = DateTime.Now, Year = DateTime.Now.Year };
                _client.GetPositionChangeNOCompleted += new EventHandler<WordActionService.GetPositionChangeNOCompletedEventArgs>(_client_GetPositionChangeNOCompleted);
                //{
                //    _clientEntity.NO = e.Result + 1;
                //    DataBinding();
                //};
                _client.GetPositionChangeNOAsync((int)_clientEntity.Year, "init");
            }
        }

        void _client_GetPositionChangeNOCompleted(object sender, WordActionService.GetPositionChangeNOCompletedEventArgs e)
        {
            string us = e.UserState.ToString();
            switch (us)
            {
                case "init":
                        _clientEntity.NO = e.Result + 1;
                        DataBinding();
                    break;
                case "validate":
                    if (_clientEntity.NO > e.Result)
                    {
                        var entity = CommunitySystem.Model.Silverlight.EntityConvertor.Convert<WordActionService.Word_PostionChange>(_clientEntity);
                        _client.SaveWord_PostionChangeAsync(entity);
                    }
                    else
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("文号必须大于" + e.Result);
                    }
                        
                    break;
            }
        }

        private void DataBinding()
        {
            this.DataContext = _clientEntity;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            Validate();
            if (errorCount > 0)
                return;
            var entity = CommunitySystem.Model.Silverlight.EntityConvertor.Convert<WordActionService.Word_PostionChange>(_clientEntity);
            if (entity.ID > 0 && entity.Year == oldYear && entity.NO == oldNO)
            {
                _client.SaveWord_PostionChangeAsync(entity);
            }
            else
            {
                _client.GetPositionChangeNOAsync((int)entity.Year, "validate");
            }
            
            //this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void Validate()
        {
            BindingExpression beUsername = tbName.GetBindingExpression(TextBox.TextProperty);
            beUsername.UpdateSource();
            beUsername = tbOrgDepart.GetBindingExpression(TextBox.TextProperty);
            beUsername.UpdateSource();
            beUsername = tbOrgPotion.GetBindingExpression(TextBox.TextProperty);
            beUsername.UpdateSource();
            beUsername = tbReason.GetBindingExpression(TextBox.TextProperty);
            beUsername.UpdateSource();
            beUsername = tbNewDepart.GetBindingExpression(TextBox.TextProperty);
            beUsername.UpdateSource();
            beUsername = tbAssgin.GetBindingExpression(TextBox.TextProperty);
            beUsername.UpdateSource();
            beUsername = tbPerson.GetBindingExpression(TextBox.TextProperty);
            beUsername.UpdateSource();
            beUsername = tbYear.GetBindingExpression(TextBox.TextProperty);
            beUsername.UpdateSource();
            beUsername = tbNO.GetBindingExpression(TextBox.TextProperty);
            beUsername.UpdateSource();
            beUsername = dpDate.GetBindingExpression(DatePicker.SelectedDateProperty);
            beUsername.UpdateSource();
        }
    }
}

